﻿using System;
using System.Xml.Linq;
using SPContrib.Artifacts.Collections;
using SPContrib.Extensions;

namespace SPContrib.Artifacts.Definitions
{
    /// <summary>
    /// Definition of a FieldRef (i.e. SPFieldLink) element
    /// </summary>
    public class FieldRefDefinition
    {
        private FieldRefDefinitionCollection _parent;

        /// <summary>
        /// The display name (i.e. the name that shows up in the UI)
        /// </summary>
        /// <remarks>xml: DisplayName</remarks>
        public string DisplayName { get; private set; }
        /// <summary>
        /// The unique identifier
        /// </summary>
        /// <remarks>xml: ID</remarks>
        public Guid Id { get; private set; }
        /// <summary>
        /// The internal name
        /// </summary>
        /// <remarks>xml: Name</remarks>
        public string Name { get; private set; }
        /// <summary>
        /// Indicates if the field will be removed or included
        /// </summary>
        /// <remarks>xml: RemoveFieldRef or FieldRef</remarks>
        public bool Remove { get; private set; }
        /// <summary>
        /// Indicates if the field is required
        /// </summary>
        /// <remarks>xml: Required</remarks>
        public bool Required { get; private set; }
        /// <summary>
        /// Indicates if the field will show up in the display form
        /// </summary>
        /// <remarks>xml: ShowInDisplayForm</remarks>
        public bool ShowInDisplayForm { get; private set; }
        /// <summary>
        /// Indicates if the field will show up in the edit form
        /// </summary>
        /// <remarks>xml: ShowInEditForm</remarks>
        public bool ShowInEditForm { get; private set; }
        /// <summary>
        /// Indicates if the field will show up in the list settings form
        /// </summary>
        /// <remarks>xml: ShowInListSettings</remarks>
        public bool ShowInListSettings { get; private set; }
        /// <summary>
        /// Indicates if the field will show up in the new item form
        /// </summary>
        /// <remarks>xml: ShowInNewForm</remarks>
        public bool ShowInNewForm { get; private set; }

        internal FieldRefDefinition(FieldRefDefinitionCollection parent, XElement fieldref)
        {
            this._parent = parent;
            this.Remove = fieldref.Name.LocalName == "RemoveFieldRef" ? true : false;
            this.Id = fieldref.Attribute<string>("ID").AsGuid();
            this.Name = fieldref.Attribute<string>("Name");
            this.DisplayName = fieldref.Attribute<string>("DisplayName", this.Name);
            this.Required = fieldref.Attribute<bool>("Required", false);
            this.ShowInDisplayForm = fieldref.Attribute<bool>("ShowInDisplayForm", true);
            this.ShowInEditForm = fieldref.Attribute<bool>("ShowInEditForm", true);
            this.ShowInListSettings = fieldref.Attribute<bool>("ShowInListSettings", true);
            this.ShowInNewForm = fieldref.Attribute<bool>("ShowInNewForm", true);
            
        }
    }
}
